package com.ljw.test;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.ljw.test.model.Child;
import com.ljw.test.model.Parent;
import com.ljw.test.util.GreenDaoManager;
import com.ljw.test.view.ChildAdapter;
import com.ljw.test.view.WordWrapView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;

public class NewKeyboard extends InputMethodService implements KeyboardView.OnKeyboardActionListener {

    private long inputTime;
    private KeyboardView keyboardView;//keyboardのView
    private LinearLayout candidatesCsvView;//予測変換のView
    private WordWrapView candidatesView;//予測変換のView

    private Keyboard keyboard, keyboard2, keyboard3;//ja/en/123のkeyboard
    private final int KEYCODE_ADD = 300;
    private final int KEYCODE_REMOVE = 301;

    private int keyType = 0;//keyboradの種類の変更
    private int deff = 0;//中央:0 左:10 上:20 右:30 下:40
    private float tapX = 0;//タッチ点のX座標
    private float tapY = 0;//タッチ点のY座標
    private int predeff = 0;//1つ前のdeffの値を保存
    private int precode = 0;//1つ前のprimaryCode
    private int multi = 0;//マルチタップ時の値増加量を指定
    private int count =0;//利用率
    private int flag;//利用
    private StringBuilder sb = new StringBuilder();

   // private final static String LOG_FILE = "Horizontal.csv";//内部ストレージに保存するcsvファイル名の指定

    private HashMap<Integer, String> mojiMap = new HashMap<Integer, String>();
    private CheckBox expandButton;
    private Toast toast;
   // private CheckBox expandCsvButton;
    private boolean check,checkifstart=true,checkifcontinue=false,ban=true;
    private boolean mode1=true,onkeymode,mode2=false;
    private String nowwrite ="",getNowsb;
    private char forcheck;
    private Switch s1,s2,t1,t2;
    private HorizontalScrollView csvLayout;
    private LinearLayout sentimentview1,sentimentview2;
    private Animation display;
    private Animation hide;
    private GreenDaoManager greenDaoManager;
    private ChildAdapter childAdapter;
    private Map<String, List<String>> csvMap;
    private Map<String,Double> sentimentMap;
    private Map<String,Double> sentimenttokanaMap;
    private Map<String,Double> csvsentimentMap;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            keyboardView.setVisibility(View.VISIBLE);
            candidatesView.requestLayout();
        }
    };

    {
        HashMap<Integer, String> m = mojiMap;
        m.put(1000, "あ");
        m.put(1010, "い");
        m.put(1020, "う");
        m.put(1030, "え");
        m.put(1040, "お");
        m.put(1001, "ぁ");
        m.put(1011, "ぃ");
        m.put(1021, "ぅ");
        m.put(1031, "ぇ");
        m.put(1041, "ぉ");
        m.put(1022, "ヴ");
        m.put(1100, "か");
        m.put(1110, "き");
        m.put(1120, "く");
        m.put(1130, "け");
        m.put(1140, "こ");
        m.put(1101, "が");
        m.put(1111, "ぎ");
        m.put(1121, "ぐ");
        m.put(1131, "げ");
        m.put(1141, "ご");
        m.put(1200, "さ");
        m.put(1210, "し");
        m.put(1220, "す");
        m.put(1230, "せ");
        m.put(1240, "そ");
        m.put(1201, "ざ");
        m.put(1211, "じ");
        m.put(1221, "ず");
        m.put(1231, "ぜ");
        m.put(1241, "ぞ");
        m.put(1300, "た");
        m.put(1310, "ち");
        m.put(1320, "つ");
        m.put(1330, "て");
        m.put(1340, "と");
        m.put(1301, "だ");
        m.put(1311, "ぢ");
        m.put(1321, "っ");
        m.put(1331, "で");
        m.put(1341, "ど");
        m.put(1322, "づ");
        m.put(1400, "は");
        m.put(1410, "ひ");
        m.put(1420, "ふ");
        m.put(1430, "へ");
        m.put(1440, "ほ");
        m.put(1401, "ば");
        m.put(1411, "び");
        m.put(1421, "ぶ");
        m.put(1431, "べ");
        m.put(1441, "ぼ");
        m.put(1402, "ぱ");
        m.put(1412, "ぴ");
        m.put(1422, "ぷ");
        m.put(1432, "ぺ");
        m.put(1442, "ぽ");
        m.put(1500, "や");
        m.put(1510, "「");
        m.put(1520, "ゆ");
        m.put(1530, "」");
        m.put(1540, "よ");
        m.put(1501, "ゃ");
        m.put(1511, "「");
        m.put(1521, "ゅ");
        m.put(1531, "」");
        m.put(1541, "ょ");
        m.put(1600, "a");
        m.put(1610, "b");
        m.put(1620, "c");
        m.put(1630, "#");
        m.put(1640, "2");
        m.put(1601, "A");
        m.put(1611, "B");
        m.put(1621, "C");
        m.put(1631, "#");
        m.put(1641, "2");
        m.put(1700, "d");
        m.put(1710, "e");
        m.put(1720, "f");
        m.put(1730, "%");
        m.put(1740, "3");
        m.put(1701, "D");
        m.put(1711, "E");
        m.put(1721, "F");
        m.put(1731, "%");
        m.put(1741, "3");
        m.put(1800, "g");
        m.put(1810, "h");
        m.put(1820, "i");
        m.put(1830, "(");
        m.put(1840, "4");
        m.put(1801, "G");
        m.put(1811, "H");
        m.put(1821, "I");
        m.put(1831, "(");
        m.put(1841, "4");
        m.put(1900, "j");
        m.put(1910, "k");
        m.put(1920, "l");
        m.put(1930, "&");
        m.put(1940, "5");
        m.put(1901, "J");
        m.put(1911, "K");
        m.put(1921, "L");
        m.put(1931, "&");
        m.put(1941, "5");
        m.put(2000, "m");
        m.put(2010, "n");
        m.put(2020, "o");
        m.put(2030, ")");
        m.put(2040, "6");
        m.put(2001, "M");
        m.put(2011, "N");
        m.put(2021, "O");
        m.put(2031, ")");
        m.put(2041, "6");
        m.put(2100, "p");
        m.put(2110, "q");
        m.put(2120, "r");
        m.put(2130, "s");
        m.put(2140, "7");
        m.put(2101, "P");
        m.put(2111, "Q");
        m.put(2121, "R");
        m.put(2131, "S");
        m.put(2141, "7");
        m.put(2200, "t");
        m.put(2210, "u");
        m.put(2220, "v");
        m.put(2230, "=");
        m.put(2240, "8");
        m.put(2201, "T");
        m.put(2211, "U");
        m.put(2221, "V");
        m.put(2231, "=");
        m.put(2241, "8");
        m.put(2300, "w");
        m.put(2310, "x");
        m.put(2320, "y");
        m.put(2330, "z");
        m.put(2340, "9");
        m.put(2301, "W");
        m.put(2311, "X");
        m.put(2321, "Y");
        m.put(2331, "Z");
        m.put(2341, "9");
        m.put(2400, "わ");
        m.put(2410, "を");
        m.put(2420, "ん");
        m.put(2430, "ー");
        m.put(2440, "");
        m.put(2401, "ゎ");
        m.put(2411, "を");
        m.put(2421, "ん");
        m.put(2431, "ー");
        m.put(2441, "");
        m.put(2500, "な");
        m.put(2510, "に");
        m.put(2520, "ぬ");
        m.put(2530, "ね");
        m.put(2540, "の");
        m.put(2600, "ま");
        m.put(2610, "み");
        m.put(2620, "む");
        m.put(2630, "め");
        m.put(2640, "も");
        m.put(2700, "ら");
        m.put(2710, "り");
        m.put(2720, "る");
        m.put(2730, "れ");
        m.put(2740, "ろ");
        m.put(2800, "'");
        m.put(2810, "@");
        m.put(2820, "/");
        m.put(2830, "_");
        m.put(2840, "1");
        m.put(2900, "4");
        m.put(2910, "[");
        m.put(2920, "`");
        m.put(2930, "]");
        m.put(2940, "'");
        m.put(3000, "5");
        m.put(3010, "<");
        m.put(3020, "$");
        m.put(3030, ">");
        m.put(3040, "\u00A5");
        m.put(3100, "6");
        m.put(3110, "{");
        m.put(3120, "&");
        m.put(3130, "}");
        m.put(3140, "\"");
        m.put(3200, "、");
        m.put(3210, "。");
        m.put(3220, "？");
        m.put(3230, "！");
        m.put(3240, "");
        m.put(3300, "-");
        m.put(3310, "+");
        m.put(3320, "0");
        m.put(3330, "*");
        m.put(3340, "");
        m.put(3400, ",");
        m.put(3410, ".");
        m.put(3420, "?");
        m.put(3430, "!");
        m.put(3440, "");
        m.put(3500, "1");
        m.put(3510, ".");
        m.put(3520, "@");
        m.put(3530, "-");
        m.put(3540, "");
        m.put(3600, "2");
        m.put(3610, "/");
        m.put(3620, ":");
        m.put(3630, "_");
        m.put(3640, "");
        m.put(3700, "3");
        m.put(3710, "~");
        m.put(3720, "%");
        m.put(3730, "^");
        m.put(3740, "");
        m.put(3800, "7");
        m.put(3810, "\\");
        m.put(3820, "");
        m.put(3830, "|");
        m.put(3840, "");
        m.put(3900, "8");
        m.put(3910, "(");
        m.put(3920, "");
        m.put(3930, ")");
        m.put(3940, "");
        m.put(4000, "9");
        m.put(4010, "=");
        m.put(4020, "");
        m.put(4030, ";");
        m.put(4040, "");
        m.put(4100, "*");
        m.put(4110, "!");
        m.put(4120, "");
        m.put(4130, "?");
        m.put(4140, "");
        m.put(4200, "0");
        m.put(4210, "");
        m.put(4220, "");
        m.put(4230, "");
        m.put(4240, "");
        m.put(4300, "#");
        m.put(4310, ",");
        m.put(4320, "+");
        m.put(4330, ".");
        m.put(4340, "");
    }

    //現在時刻をyyyy/MM/dd HH:mm:ss形式で取得する
    public static String getNowDate() {
        final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        final Date date = new Date(System.currentTimeMillis());
        return df.format(date);
    }

    //現在時刻をミリ秒単位で取得
    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        greenDaoManager = GreenDaoManager.getInstance();
        initCSV();
        initsentimentCSV();
        initsentimenttokanaCSV();
        initcsvsentiment();
        /*if(toast==null){

        }else {

            button1(sentimentview2);
            button2(sentimentview2);
            button3(sentimentview2);
            button4(sentimentview2);
            button5(sentimentview2);
            button6(sentimentview2);
            button7(sentimentview2);
            button8(sentimentview2);
            button9(sentimentview2);
            btn1(sentimentview1);
            btn2(sentimentview1);
            btn3(sentimentview1);
            btn4(sentimentview1);
            btn5(sentimentview1);
            btn6(sentimentview1);
            btn7(sentimentview1);
            btn8(sentimentview1);
            btn9(sentimentview1);
        }*/



        System.out.println(sentimentMap);
        System.out.println(sentimenttokanaMap);
        System.out.println(csvsentimentMap);
        System.out.println(csvMap);
    }



    //キーボード
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View onCreateInputView() {
        super.onCreateInputView();

        display = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set_display);
        hide = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set_hide);

        LinearLayout frameLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.keyboard_view, null);

        candidatesView = frameLayout.findViewById(R.id.word_wrap_view);
        keyboardView = frameLayout.findViewById(R.id.keyboard);
        keyboardView.post(new Runnable() {
            @Override
            public void run() {
                candidatesView.setKeyBoardHeight(keyboardView.getHeight());
                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) keyboardView.getLayoutParams();
                layoutParams.topMargin = candidatesView.getRowHeight();
                onKey(Keyboard.KEYCODE_DELETE, new int[]{});
            }
        });

        candidatesCsvView = frameLayout.findViewById(R.id.csv_view);
        csvLayout = frameLayout.findViewById(R.id.csv_layout);
        candidatesCsvView = frameLayout.findViewById(R.id.csv_view);
        csvLayout = frameLayout.findViewById(R.id.csv_layout);
        sentimentview1 = frameLayout.findViewById(R.id.sentiment_layout);
        sentimentview2 = frameLayout.findViewById(R.id.sentiment_layout2);
        t1 = (Switch) sentimentview2.findViewById(R.id.switch4);
        t2 = (Switch) sentimentview1.findViewById(R.id.switch3);
        t1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(t1.isChecked()){
                    t2.setChecked(true);
                    mode2 = true;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "プライベートモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "プライベートモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }
                    toast.show();

                }
                else{
                    t2.setChecked(false);
                    mode2=false;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "パブリックモードORプライバシーモードを選択してください", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "パブリックモードORプライバシーモードを選択してください", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }

                    toast.show();
                }
            }
        });
        t2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(t2.isChecked()){
                    t1.setChecked(true);
                    mode2 = true;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "プライベートモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "プライベートモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }
                    toast.show();

                }
                else{
                    t1.setChecked(false);
                    mode2=false;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "パブリックモードORプライバシーモードを選択してください", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "パブリックモードORプライバシーモードを選択してください", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }

                    toast.show();
                }
            }
        });
        /*toggle2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    toggle1.setChecked(true);
                    mode2=true;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "パブリックモードORプライバシーモードを選択してください", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "パブリックモードORプライバシーモードを選択してください", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }
                    toast.show();

                    // The toggle is enabled
                } else {
                    toggle1.setChecked(false);
                    mode2=false;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "プライベートモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "プライベートモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }

                    toast.show();
                    // The toggle is disabled
                }
            }
        })*/
        s1 = (Switch) sentimentview2.findViewById(R.id.switch2);
        s2 = (Switch) sentimentview1.findViewById(R.id.switch1);
        s1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(s1.isChecked()){
                    s2.setChecked(true);
                    mode1 = true;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "パブリックモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "パブリックモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }
                    toast.show();

                }
                else{
                    s2.setChecked(false);
                    mode1=false;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "プライバシーモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "プライバシーモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }

                    toast.show();
                }
            }
        });
        s2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(s2.isChecked()){
                    s1.setChecked(true);
                    mode1=true;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "パブリックモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "パブリックモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }
                    toast.show();
                }
                else{
                    s1.setChecked(false);
                    mode1=false;
                    if(toast!=null) {
                        toast.cancel();
                        toast = Toast.makeText(getApplicationContext(), "プライバシーモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }else {
                        toast = Toast.makeText(getApplicationContext(), "プライバシーモード", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                    }
                    toast.show();
                }
            }
        });


        expandButton = frameLayout.findViewById(R.id.cb_expand_button);
        expandButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                candidatesView.setState(b);
                hideKeyBoardView(b);
                //check = b;
                if(checkifcontinue==true && forcheck=='1' ) {
                    check = true;
                }else {
                    check=b;
                }
                getstate(b);
            }
        });

        expandButton.setVisibility(View.INVISIBLE);
        candidatesView.setExpandButton(expandButton);

        keyboard = new Keyboard(this, R.layout.keyboard);
        keyboard2 = new Keyboard(this, R.layout.keyboard2);
        keyboard3 = new Keyboard(this, R.layout.keyboard3);

        keyboardView.setBackgroundColor(Color.parseColor("#e1e1e1"));
        keyboardView.setKeyboard(keyboard);
        keyboardView.setOnKeyboardActionListener(this);
        keyboardView.setPreviewEnabled(false);
        keyboardView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //タッチ押下のとき
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    deff = 0;//
                    tapX = event.getX();//タッチ点のX座標の取得
                    tapY = event.getY();//タッチ点のY座標の取得
                }

                //指を持ち上げずにスライドさせた場合(フリック操作の処理)
                else if (event.getAction() == MotionEvent.ACTION_MOVE) {

                    //どの方向に指を移動させたかの計算
                    if (Math.pow(event.getX() - tapX, 2) + Math.pow(event.getY() - tapY, 2) > 1000) {
                        multi = 0;
                        if ((event.getX() - tapX < -(event.getY() - tapY)) && (event.getX() - tapX < event.getY() - tapY)) {
                            deff = 10;
                        } else if ((event.getX() - tapX < -(event.getY() - tapY)) && (event.getX() - tapX > event.getY() - tapY)) {
                            deff = 20;
                        }
                        if ((event.getX() - tapX > -(event.getY() - tapY)) && (event.getX() - tapX > event.getY() - tapY)) {
                            deff = 30;
                        } else if ((event.getX() - tapX > -(event.getY() - tapY)) && (event.getX() - tapX < event.getY() - tapY)) {
                            deff = 40;
                        }
                        return true;
                    }
                    deff = 0;
                }
                return false;
            }
        });

        return frameLayout;
    }

    private void search(String sb) {
        List<Child> candidatesList = new ArrayList<>();
        List<Child> children = new ArrayList<>();

        for (int i = sb.length(); i > 0; i--) {
            final String s = sb.substring(0, i);
//            candidatesList.add(new Child(s, s.length()));
//            candidatesList.add(new Child(convertToKata(s), s.length()));

            List<Child> okuList = new ArrayList<>();
            String okurikana = okurikana(s);
            if (okurikana != null) {
                Parent okurikanaParent = greenDaoManager.queryChildren(okurikana);
                System.out.println("oku:" + okurikana);
                if (okurikanaParent != null) {
                    List<Child> oList = okurikanaParent.getChildList();
                    for (Child child : oList) {
                        child.setPreLength(s.length() - 1);
                        okuList.add(child);
                    }
                }
            }

            Parent parent = greenDaoManager.queryChildren(s);
            if (parent == null) {
                candidatesList.add(new Child(s, s.length()));
                candidatesList.add(new Child(convertToKata(s), s.length()));
                candidatesList.addAll(okuList);
                Collections.sort(candidatesList, new Comparator<Child>() {
                    @Override
                    public int compare(Child t1, Child t2) {
                        return t2.getTime() - t1.getTime();
                    }
                });
                continue;
            }


            LinkedList<Child> list = new LinkedList<>();
            List<Child> childList = parent.getChildList();
            Child c1 = new Child(null, parent.getId(), s, 0);
            Child c2 = new Child(null, parent.getId(), convertToKata(s), 0);

            boolean flag1 = false;
            boolean flag2 = false;
            for (Child child : childList) {
                System.out.println(child.toString());
                if (child.getChild().equals(c1.getChild())) flag1 = true;
                if (child.getChild().equals(c2.getChild())) flag2 = true;
            }
            if (!flag1) list.add(c1);
            if (!flag2) list.add(c2);


            list.addAll(childList);
            for (Child child : list) {
                child.setPreLength(s.length());
            }

            list.addAll(okuList);

            Collections.sort(list, new Comparator<Child>() {
                @Override
                public int compare(Child t1, Child t2) {
                    return t2.getTime() - t1.getTime();
                }
            });

            candidatesList.addAll(list);

            if (csvMap.containsKey(sb)) {
                List<String> strings = csvMap.get(sb);
                System.out.println("dsd"+csvMap.get(sb));
                System.out.println(Arrays.toString(strings.toArray()));
                for (String string : strings) {
                    System.out.println(string);
                    children.add(new Child(string, sb.length()));

                }
                csvLayout.setVisibility(View.VISIBLE);
                sentimentview1.setVisibility(View.VISIBLE);
                sentimentview2.setVisibility(View.GONE);

            }else{
            }
        }

        removeDupElement(children);
        addButton(candidatesView, candidatesList);
        addButton(candidatesCsvView, children);
    }

    private void initCSV() {
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(this.getAssets().open("candidates2.csv")));

            csvMap = new HashMap<>();
            String str;
            while ((str = br.readLine()) != null) {
                String[] split = str.split(",");
                ArrayList<String> list = new ArrayList<>();
                for (int i = 2; i < split.length; i++) {
                    list.add(split[i]);
                }
                csvMap.put(split[0], list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initsentimentCSV() {
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(this.getAssets().open("sentiment2.csv")));

            sentimentMap = new HashMap<>();
            String str;
            while ((str = br.readLine()) != null) {
                String[] split = str.split(",");
                String select = split[0];
                Double value = Double.parseDouble(split[1]) ;
                sentimentMap.put(select, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initsentimenttokanaCSV() {
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(this.getAssets().open("sentimenttokana.csv")));

            sentimenttokanaMap = new HashMap<>();
            String str;
            while ((str = br.readLine()) != null) {
                String[] split = str.split(",");
                String select = split[0];
                Double value = Double.parseDouble(split[1]) ;
                sentimenttokanaMap.put(select, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initcsvsentiment() {
        try {

            BufferedReader br = new BufferedReader(new InputStreamReader(this.getAssets().open("csvsentiment.csv")));

            csvsentimentMap = new HashMap<>();
            String str;
            while ((str = br.readLine()) != null) {
                String[] split = str.split(",");
                String select = split[0];
                Double value = Double.parseDouble(split[1]) ;
                csvsentimentMap.put(select, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void addButton(final ViewGroup group, final List<Child> list) {

        if (group.getChildCount() != 0) group.removeAllViews();

        for (int i = 0; i < list.size(); i++) {
            final Child child = list.get(i);

            final TextView button = new TextView(this);
            button.setText(child.getChild());
            button.setTextSize(19);
            button.setId(i);
            button.setPadding(25, 0, 25, 0);
            button.setTextColor(Color.BLACK);
            button.setBackgroundResource(R.drawable.button);
            button.setTag(child);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    InputConnection ic = getCurrentInputConnection();
                    ic.commitText(((TextView) view).getText(), 1);

                    csvLayout.setVisibility(View.INVISIBLE);
                    sentimentview1.setVisibility(View.GONE);
                    sentimentview2.setVisibility(View.VISIBLE);


                    candidatesView.removeAllViews();
                    candidatesCsvView.removeAllViews();
                    candidatesView.scrollTo(0, 0);
                    if (check == true) {
                        forcheck = '1';
                    } else {
                        forcheck = '0';

                    }

                    Child tag = (Child) button.getTag();
                    System.out.println("tag"+((Child) button.getTag()).getChild());


                    tag.setTime(tag.getTime() + 1);
                    if (greenDaoManager.queryHasChild(tag)) {
                        greenDaoManager.updateChildrenData(tag);
                       // System.out.println(tag.toString());
                    } else {
                        System.out.println(tag.getParentId() + "id");
                        if (group instanceof WordWrapView && tag.getParentId() != null) {
                            greenDaoManager.insertChildrenData(tag);
                        } else if (group instanceof LinearLayout) {

                        }
                    }
                    getNowsb=sb.toString();
                    if (group instanceof LinearLayout) {
                        sb.delete(0, tag.getPreLength()+1);
                    }
                    else if(group instanceof WordWrapView){
                        sb.delete(0,tag.getPreLength());
                    }
                    else{
                        sb.delete(0,tag.getPreLength());
                    }
                    if (sb.length() > 0) {
                        nowwrite += ((Child) button.getTag()).getChild();
                        checkifcontinue=true;

                        ic.setComposingText(sb.toString(), 1);
                        search(sb.toString());
                    }
                    else{
                        ban = false;
                        nowwrite += ((Child) button.getTag()).getChild();
                        checkifcontinue=false;
                        if(checkifstart==true){


                            checkifstart=false;


                        }
                        if(checkifstart==false){
                            if(((Child) button.getTag()).getChild().equals("。") ||((Child) button.getTag()).getChild().equals("!")||((Child) button.getTag()).getChild().equals("?")){
                                System.out.println("ok");
                                Calendar cal = Calendar.getInstance();
                                int year = cal.get(Calendar.YEAR);
                                int month = cal.get(Calendar.MONTH);// 0 - 11
                                int day = cal.get(Calendar.DAY_OF_MONTH);
                                int hour = cal.get(Calendar.HOUR_OF_DAY);
                                int minute = cal.get(Calendar.MINUTE);
                                int second = cal.get(Calendar.SECOND);
                                String date = year + "/" + (month + 1) + "/" + day + "/" + hour + "/" + minute + "/" + second;
                                System.out.println(nowwrite);
                                String txt = date +","+forcheck+","+tag.getChild()+","+"0"+ "."+"\n" ;
                                if(mode2==true) {
                                }
                                else {
                                    if (mode1 == true) {
                                        writeinphone(txt);
                                    } else {
                                        Recoardsentiment(txt);
                                    }
                                }

                                nowwrite="";
                                checkifstart=true;
                                ban=true;


                            }else{
                                if(mode2==true){

                                }
                                else {
                                    if (mode1 == true) {
                                        if (group instanceof LinearLayout) {
                                            count += 1;
                                            flag = 1;
                                        } else {
                                            flag = 0;
                                        }
                                        String date = getdate();
                                        String txt = date + "," + forcheck + ',' + tag.getChild() + "," + flag + ".";
                                        writeinphone(txt);
                                    } else {
                                        System.out.println("no");
                                        if (group instanceof LinearLayout) {
                                            flag = 1;
                                            String before = "";
                                            String after = "";
                                            if (sentimenttokanaMap.containsKey(getNowsb)) {

                                                System.out.println(getNowsb);
                                                if (sentimenttokanaMap.get(getNowsb) <= 1.0 && sentimenttokanaMap.get(getNowsb) > 0.9) {
                                                    before = "+10.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.9 && sentimenttokanaMap.get(getNowsb) > 0.8) {
                                                    before = "+9.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.8 && sentimenttokanaMap.get(getNowsb) > 0.7) {
                                                    before = "+8.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.7 && sentimenttokanaMap.get(getNowsb) > 0.6) {
                                                    before = "+7.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.6 && sentimenttokanaMap.get(getNowsb) > 0.5) {
                                                    before = "+6.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.5 && sentimenttokanaMap.get(getNowsb) > 0.4) {
                                                    before = "+5.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.4 && sentimenttokanaMap.get(getNowsb) > 0.3) {
                                                    before = "+4,0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.3 && sentimenttokanaMap.get(getNowsb) > 0.2) {
                                                    before = "+3.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.2 && sentimenttokanaMap.get(getNowsb) > 0.1) {
                                                    before = "+2.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) <= 0.1 && sentimenttokanaMap.get(getNowsb) > 0.0) {
                                                    before = "+1.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.1 && sentimenttokanaMap.get(getNowsb) <= 0.0) {
                                                    before = "-1.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.2 && sentimenttokanaMap.get(getNowsb) <= -0.1) {
                                                    before = "-2.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.3 && sentimenttokanaMap.get(getNowsb) <= -0.2) {
                                                    before = "-3.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.4 && sentimenttokanaMap.get(getNowsb) <= -0.3) {
                                                    before = "-4.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.5 && sentimenttokanaMap.get(getNowsb) <= -0.4) {
                                                    before = "-5.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.6 && sentimenttokanaMap.get(getNowsb) <= -0.5) {
                                                    before = "-6.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.7 && sentimenttokanaMap.get(getNowsb) <= -0.6) {
                                                    before = "-7.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.8 && sentimenttokanaMap.get(getNowsb) <= -0.7) {
                                                    before = "-8.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) > -0.9 && sentimenttokanaMap.get(getNowsb) <= -0.8) {
                                                    before = "-9.0";
                                                } else if (sentimenttokanaMap.get(getNowsb) >= -1.0 && sentimenttokanaMap.get(getNowsb) <= -0.9) {
                                                    before = "-10.0";
                                                }


                                            } else {
                                                before = "0.0";
                                            }
                                            String date = getdate();
                                            String txt = date + "," + forcheck + ',' + before + '→';
                                            Recoardsentiment(txt);


                                            if (csvsentimentMap.containsKey(tag.getChild())) {
                                                System.out.println(tag.getChild());
                                                System.out.println(csvsentimentMap.get(tag.getChild()));
                                                if (csvsentimentMap.get(tag.getChild()) <= 10.0 && csvsentimentMap.get(tag.getChild()) > 9.0) {
                                                    after = "+10.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 9.0 && csvsentimentMap.get(tag.getChild()) > 8.0) {
                                                    after = "+9.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 8.0 && csvsentimentMap.get(tag.getChild()) > 7.0) {
                                                    after = "+8.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 7.0 && csvsentimentMap.get(tag.getChild()) > 6.0) {
                                                    after = "+7.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 6.0 && csvsentimentMap.get(tag.getChild()) > 5.0) {
                                                    //System.out.println("okokok");
                                                    after = "+6.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 5.0 && csvsentimentMap.get(tag.getChild()) > 4.0) {
                                                    after = "+5.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 4.0 && csvsentimentMap.get(tag.getChild()) > 3.0) {
                                                    after = "+4.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 3.0 && csvsentimentMap.get(tag.getChild()) > 2.0) {
                                                    after = "+3.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 2.0 && csvsentimentMap.get(tag.getChild()) > 1.0) {
                                                    after = "+2.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) <= 1.0 && csvsentimentMap.get(tag.getChild()) > 0.0) {
                                                    after = "+1.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -1.0 && csvsentimentMap.get(tag.getChild()) <= 0.0) {
                                                    after = "-1.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -2.0 && csvsentimentMap.get(tag.getChild()) <= -1.0) {
                                                    after = "-2.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -3.0 && csvsentimentMap.get(tag.getChild()) <= -2.0) {
                                                    after = "-3.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -4.0 && csvsentimentMap.get(tag.getChild()) <= -3.0) {
                                                    after = "-4.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -5.0 && csvsentimentMap.get(tag.getChild()) <= -4.0) {
                                                    after = "-5.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -6.0 && csvsentimentMap.get(tag.getChild()) <= -5.0) {
                                                    after = "-6.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -7.0 && csvsentimentMap.get(tag.getChild()) <= -6.0) {
                                                    after = "-7.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -8.0 && csvsentimentMap.get(tag.getChild()) <= -7.0) {
                                                    after = "-8.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) > -9.0 && csvsentimentMap.get(tag.getChild()) <= -8.0) {
                                                    after = "-9.0";
                                                } else if (csvsentimentMap.get(tag.getChild()) >= -10.0 && csvsentimentMap.get(tag.getChild()) <= -9.0) {
                                                    after = "-10.0";
                                                }


                                            } else {
                                                after = "0.0";
                                            }
                                            System.out.println(after);
                                            System.out.println(nowwrite);
                                            String txt1 = after + "," + flag + ".";
                                            Recoardsentiment(txt1);
                                            nowwrite = "";


                                            //System.out.println(getNowsb);
                                        } else if (group instanceof WordWrapView) {
                                            flag = 0;
                                            String genernal = "";
                                            if (sentimentMap.containsKey(nowwrite)) {
                                                if (sentimentMap.get(nowwrite) <= 1.0 && sentimentMap.get(nowwrite) > 0.9) {
                                                    genernal = "+10.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.9 && sentimentMap.get(nowwrite) > 0.8) {
                                                    genernal = "+9.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.8 && sentimentMap.get(nowwrite) > 0.7) {
                                                    genernal = "+8.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.7 && sentimentMap.get(nowwrite) > 0.6) {
                                                    genernal = "+7.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.6 && sentimentMap.get(nowwrite) > 0.5) {
                                                    genernal = "+6.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.5 && sentimentMap.get(nowwrite) > 0.4) {
                                                    genernal = "+5.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.4 && sentimentMap.get(nowwrite) > 0.3) {
                                                    genernal = "+4.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.3 && sentimentMap.get(nowwrite) > 0.2) {
                                                    genernal = "+3.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.2 && sentimentMap.get(nowwrite) > 0.1) {
                                                    genernal = "+2.0";
                                                } else if (sentimentMap.get(nowwrite) <= 0.1 && sentimentMap.get(nowwrite) > 0.0) {
                                                    genernal = "+1.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.1 && sentimentMap.get(nowwrite) <= 0.0) {
                                                    genernal = "-1.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.2 && sentimentMap.get(nowwrite) <= -0.1) {
                                                    genernal = "-2.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.3 && sentimentMap.get(nowwrite) <= -0.2) {
                                                    genernal = "-3.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.4 && sentimentMap.get(nowwrite) <= -0.3) {
                                                    genernal = "-4.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.5 && sentimentMap.get(nowwrite) <= -0.4) {
                                                    genernal = "-5.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.6 && sentimentMap.get(nowwrite) <= -0.5) {
                                                    genernal = "-6.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.7 && sentimentMap.get(nowwrite) <= -0.6) {
                                                    genernal = "-7.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.8 && sentimentMap.get(nowwrite) <= -0.7) {
                                                    genernal = "-8.0";
                                                } else if (sentimentMap.get(nowwrite) > -0.9 && sentimentMap.get(nowwrite) <= -0.8) {
                                                    genernal = "-9.0";
                                                } else if (sentimentMap.get(nowwrite) >= -1.0 && sentimentMap.get(nowwrite) <= -0.9) {
                                                    genernal = "-10.0";
                                                }
                                                // System.out.println(nowwrite);


                                            } else {
                                                genernal = "0.0";
                                            }
                                            if (checkifcontinue == true) {
                                                System.out.println(nowwrite);
                                                System.out.println("noRecord");
                                            } else {
                                                String date = getdate();
                                                String txt = date + "," + forcheck + ',' + genernal + "," + flag + ".";
                                                System.out.println(nowwrite);
                                                Recoardsentiment(txt);
                                                nowwrite = "";
                                            }


                                        } else {
                                            System.out.println("something wrong");
                                        }
                                    }
                                }
                            }

                        }



                    }

                    /*if(checkifcontinueselect==true) {
                        nowwrite = ((Child) button.getTag()).getChild();
                    }else {
                        nowwrite += ((Child) button.getTag()).getChild();
                        System.out.println(forcheck);
                        System.out.println(tag.toString());
                        System.out.println("aaaa" + sentimentMap.get(((Child) button.getTag()).getChild()));
                        System.out.println("bbbb" + csvMap.get(((Child) button.getTag()).getChild()));




                        // SDカードのパスをチェック


                        System.out.println("ccc" + checkifcontinueselect);
                    }*/
                }
            });
            group.addView(button);
        }
    }


    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
        EditorInfo info = getCurrentInputEditorInfo();
        String logString;

        csvLayout.setVisibility(View.INVISIBLE);
        sentimentview1.setVisibility(View.GONE);
        sentimentview2.setVisibility(View.VISIBLE);
        candidatesCsvView.removeAllViews();
        candidatesView.removeAllViews();
        candidatesView.scrollTo(0, 0);
        switch (primaryCode) {
            case KeyEvent.KEYCODE_HOME://何もないキー
                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,nothing,\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                break;
            case KeyEvent.KEYCODE_STAR://キーボードの切り替え
                if (keyType == 0) {
                    keyboardView.setKeyboard(keyboard2);
                }
                if (keyType == 1) {
                    keyboardView.setKeyboard(keyboard3);
                }
                if (keyType == 2) {
                    keyboardView.setKeyboard(keyboard);
                }
                keyType = (keyType + 1) % 3;
                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,change,\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT://カーソルを左に移動
                if (!(sb.length() > 0)) {
                    ic.commitText("", -1);
                    //Log.d("pressed", "left");
                }
                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,left,\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT://カーソルを右に移動
                if (!(sb.length() > 0)) {
                    ic.commitText("", 2);
                    //Log.d("pressed", "right");
                }
                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,right,\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                break;
            case Keyboard.KEYCODE_DELETE://バックスペース
                expandButton.setVisibility(View.INVISIBLE);

                CharSequence currentText = ic.getExtractedText(new ExtractedTextRequest(), 0).text;
                int length = ic.getTextBeforeCursor(currentText.length(), 0).length();
                if (length > 0) {
                    if (sb.length() > 0) {
                        sb.deleteCharAt(sb.length() - 1);
                        ic.setComposingText(sb.toString(), 1);
                    } else {
                        ic.deleteSurroundingText(1, 0);
                    }
                }
                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,delete,\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                break;
            case KeyEvent.KEYCODE_ENTER://リターン
                int type = info.imeOptions & EditorInfo.IME_MASK_ACTION;
                if ((type == EditorInfo.IME_ACTION_UNSPECIFIED) || (type == EditorInfo.IME_ACTION_NONE)) {
                    if (sb.length() > 0) {
                        ic.commitText(sb.toString(), 1);
                        sb.delete(0, sb.length());
                    } else {
                        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                    }
                } else {
                    if ((info.inputType & EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE) != 0) {
                        //String string = new String(sb);
                        if (sb.length() > 0) {
                            ic.commitText(sb.toString(), 1);
                            sb.delete(0, sb.length());
                        } else {
                            ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                        }
                    } else {
                        //System.out.println("enter");
                        ic.performEditorAction(type);
                    }
                }
                if(ban==false){
                    ban=true;
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH);// 0 - 11
                    int day = cal.get(Calendar.DAY_OF_MONTH);
                    int hour = cal.get(Calendar.HOUR_OF_DAY);
                    int minute = cal.get(Calendar.MINUTE);
                    int second = cal.get(Calendar.SECOND);
                    String date = year + "/" + (month + 1) + "/" + day + "/" + hour + "/" + minute + "/" + second;
                    String  txt = date + ","+"0"+","+"。"+","+"0"+ "."+"\n" ;
                    writeinphone(txt);
                    Recoardsentiment(txt);



                    nowwrite="";
                    checkifstart=true;

                    System.out.println("enter");
                    /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,enter,\n\n";
                    try {
                        OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                        PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                        writer.append(logString);
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/
                    }
                break;
            case KeyEvent.KEYCODE_SPACE://スペース

                if (sb.length() == 0)
                    ic.commitText(" ", 1);

                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input,space/conversion,\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                break;
            case KeyEvent.KEYCODE_SHIFT_LEFT://大文字小文字，濁音半濁音の切り替え
                boolean flag = false;
                CharSequence before = ic.getTextBeforeCursor(1, InputConnection.GET_TEXT_WITH_STYLES);
                before = before.toString();
                for (int i = 1000; i <= 2440; i += 10) {
                    if (before.equals(mojiMap.get(i))) {
                        if (sb.length() > 0) {
                            sb.replace(sb.length() - 1, sb.length(), mojiMap.get(i + 1));
                            ic.setComposingText(sb.toString(), 1);
                        } else {
                            ic.deleteSurroundingText(1, 0);
                            ic.commitText(mojiMap.get(i + 1), 1);
                        }
                        /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input," + mojiMap.get(i + 1) + ",\n";
                        try {
                            OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                            PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                            writer.append(logString);
                            writer.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }*/
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    for (int i = 1001; i <= 2431; i += 10) {
                        if (before.equals(mojiMap.get(i))) {
                            //文字の切り替えが2パターンある文字列(ヴ,つ,は行)
                            if ((i >= 1401 && i <= 1441) || i == 1321 || i == 1021) {
                                if (sb.length() > 0) {
                                    sb.replace(sb.length() - 1, sb.length(), mojiMap.get(i + 1));
                                    ic.setComposingText(sb.toString(), 1);
                                } else {
                                    ic.deleteSurroundingText(1, 0);
                                    ic.commitText(mojiMap.get(i + 1), 1);
                                }
                                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input," + mojiMap.get(i + 1) + ",\n";
                                try {
                                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                                    writer.append(logString);
                                    writer.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }*/
                            } else {
                                if (sb.length() > 0) {
                                    sb.replace(sb.length() - 1, sb.length(), mojiMap.get(i - 1));
                                    ic.setComposingText(sb.toString(), 1);
                                } else {
                                    ic.deleteSurroundingText(1, 0);
                                    ic.commitText(mojiMap.get(i - 1), 1);
                                }
                                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input," + mojiMap.get(i - 1) + ",\n";
                                try {
                                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                                    writer.append(logString);
                                    writer.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }*/
                            }
                            flag = true;
                            break;
                        }
                    }
                }
                if (!flag) {
                    for (int i = 1022; i <= 1442; i += 10) {
                        if (before.equals(mojiMap.get(i))) {
                            if (sb.length() > 0) {
                                sb.replace(sb.length() - 1, sb.length(), mojiMap.get(i - 2));
                                ic.setComposingText(sb.toString(), 1);
                            } else {
                                ic.deleteSurroundingText(1, 0);
                                ic.commitText(mojiMap.get(i - 2), 1);
                            }
                            /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input," + mojiMap.get(i - 2) + ",\n";
                            try {
                                OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                                PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                                writer.append(logString);
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }*/
                            break;
                        }
                        if (i == 1022) {
                            i += 290;
                        } else if (i == 1322) {
                            i += 70;
                        }
                    }
                }
                break;
            case KEYCODE_ADD: {
                popupDialog(KEYCODE_ADD);
                break;
            }
            case KEYCODE_REMOVE: {
                popupDialog(KEYCODE_REMOVE);
                break;
            }
            default://文字
                if (deff == 0 && predeff == 0 && precode == primaryCode && System.currentTimeMillis() - inputTime < 700 && primaryCode < 4400) {
                    sb.delete(sb.length() - 1, sb.length());
                    if (primaryCode == 1500) {
                        multi += 20;//やゆよのみ「」を飛ばす
                    } else {
                        multi += 10;//その他は一文字ずつ飛ばす
                    }

                    if ((primaryCode == 2400) || (primaryCode >= 3200 && primaryCode <= 3700) || (primaryCode == 4300)) {
                        if (multi >= 40) multi = 0; //4つしか文字を割り当てていないキー
                    } else if (primaryCode == 4200) {
                        if (multi >= 10) multi = 0;  //1つしか文字を割り当てていないキー
                    } else if (primaryCode >= 3800 && primaryCode <= 4100) {
                        if (multi > 10 && multi < 40) multi = 30; //00 10 30に文字を割り当てているキー

                        else if (multi >= 40) multi = 0;
                    }
                    if (multi >= 50) {
                        multi = 0;
                    }
                } else {
                    multi = 0;
                }

                String chara = mojiMap.get(primaryCode + deff + multi);
                sb.append(chara);
                ic.setComposingText(sb.toString(), 1);
                inputTime = System.currentTimeMillis();
                /*logString = getNowDate() + "," + getCurrentTimeMillis() + ",input," + chara + ",\n";
                try {
                    OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                    writer.append(logString);
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                predeff = deff;
                break;
        }

        search(sb.toString());
        precode = primaryCode;
    }

    // 平假名转化成片假名
    private String convertToKata(String hila) {
        String kata = "";
        for (int k = 0; k < hila.length(); k++) {
            char c = hila.charAt(k);
            int i = c + 96;
            char d = (char) i;
            kata = kata + d;
        }
        return kata;
    }

    private void hideKeyBoardView(boolean b) {
        if (b) {
            candidatesView.scrollTo(0, 0);
            keyboardView.setVisibility(View.INVISIBLE);
            keyboardView.startAnimation(hide);
            candidatesView.requestLayout();
        } else {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(0);
                }
            }, 300);
            keyboardView.startAnimation(display);
        }
    }
    private boolean getstate(boolean b){
        if(b){
            System.out.println("True");
            return b ;
        }
        else{
            System.out.println("False");
            return b ;
        }
    }

    public void popupDialog(final int mode) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final AlertDialog dialog = builder.create();

        View inflate = View.inflate(getApplicationContext(), R.layout.activity_add, null);
        final ViewHolder holder = new ViewHolder(inflate);

        childAdapter = new ChildAdapter(this);
        if (mode == KEYCODE_ADD) holder.title.setText("ADD");
        else holder.title.setText("REMOVE");
        holder.lvChildList.setAdapter(childAdapter);
        holder.btAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> childList = childAdapter.getChildList();
                String parentWord = holder.etAddParent.getText().toString();
                if (parentWord.equals("")) {
                    Toast.makeText(getApplicationContext(), "Please input parent word", Toast.LENGTH_SHORT).show();
                } else {
                    if (!childList.get(0).equals("")) {
                        for (String childString : childList) {
                            String logString = getNowDate() + "," + getCurrentTimeMillis() + ",add," + childString + ",\n";
                            /*try {
                                OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
                                PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
                                writer.append(logString);
                                writer.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }*/
                        }
                        dialog.dismiss();
                        if (mode == KEYCODE_ADD) add(parentWord, childList);
                        else remove(parentWord, childList);
                    } else {
                        Toast toast =Toast.makeText(getApplicationContext(), "Please input child word", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.TOP,0,0);
                        toast.show();
                    }
                }
            }
        });

        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else {
            if (Build.VERSION.SDK_INT >= 26) {
                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            } else {
                dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            }
        }

        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.y = -300;

        dialog.setView(new EditText(getApplicationContext()));
        dialog.setView(inflate);
        dialog.show();
    }

    private void add(String parentWord, List<String> childList) {
        Parent parent = greenDaoManager.queryChildren(parentWord);
        if (parent == null) {
            parent = new Parent(null, parentWord);
            greenDaoManager.insertParentData(parent);
        }

        List<Child> l = parent.getChildList();
        List<String> list = new ArrayList<>();
        for (Child child : l) {
            list.add(child.getChild());
        }

        for (int i = 0; i < childList.size(); i++) {
            String child = childList.get(i);
            if (!list.contains(child))
                greenDaoManager.insertChildrenData(new Child(null, parent.getId(), child, 0));
        }
        greenDaoManager.cleanCache();
    }

    private void remove(String parentWord, List<String> childList) {
        Parent parent = greenDaoManager.queryChildren(parentWord);
        if (parent == null) return;

        List<Child> dbChildList = parent.getChildList();

        for (String s : childList) {
            int flag = 0;
            for (int i = dbChildList.size() - 1; i >= 0; i--) {
                Child child = dbChildList.get(i);
                if (child.getChild().equals(s))
                    dbChildList.remove(i);
            }
        }

        for (Child child : dbChildList) {
            greenDaoManager.deleteChildrenData(child.getId());
        }

    }

    /*public void btn1(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude21", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude21", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"1"+"\n";
        Recoardsentiment(txt);
    }

    public void btn2(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude23", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude23", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"2"+"\n";
        Recoardsentiment(txt);
    }
    public void btn3(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude1f", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude1f", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"3"+"\n";
        Recoardsentiment(txt);
    }
    public void btn4(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude12", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude12", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"4"+"\n";
        Recoardsentiment(txt);
    }
    public void btn5(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude11", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude11", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"5"+"\n";
        Recoardsentiment(txt);
    }
    public void btn6(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0c", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0c", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"6"+"\n";
        Recoardsentiment(txt);
    }
    public void btn7(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude04", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude04", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"7"+"\n";
        Recoardsentiment(txt);
    }
    public void btn8(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude01", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude01", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"8"+"\n";
        Recoardsentiment(txt);
    }
    public void btn9(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0e", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0e", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"9"+"\n";
        Recoardsentiment(txt);
    }
    public void button1(View view) {

        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude21", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude21", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"1"+"\n";
        Recoardsentiment(txt);
    }

    public void button2(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude23", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude23", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"2"+"\n";
        Recoardsentiment(txt);
    }
    public void button3(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude1f", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude1f", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"3"+"\n";
        Recoardsentiment(txt);
    }
    public void button4(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude12", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude12", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"4"+"\n";
        Recoardsentiment(txt);
    }
    public void button5(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude11", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude11", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"5"+"\n";
        Recoardsentiment(txt);
    }
    public void button6(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0c", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0c", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"6"+"\n";
        Recoardsentiment(txt);
    }
    public void button7(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude04", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude04", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"7"+"\n";
        Recoardsentiment(txt);
    }
    public void button8(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude01", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude01", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"8"+"\n";
        Recoardsentiment(txt);
    }
    public void button9(View view) {
        if(toast!=null) {
            toast.cancel();
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0e", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);

        }else {
            toast = Toast.makeText(getApplicationContext(), "\ud83d\ude0e", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
        String date = getdate();
        String txt = date+","+"9"+"\n";
        Recoardsentiment(txt);
    }*/

    public static
    class ViewHolder {
        public View rootView;
        public EditText etAddParent;
        public ListView lvChildList;
        public Button btAdd;
        public TextView title;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.etAddParent = (EditText) rootView.findViewById(R.id.et_add_parent);
            this.lvChildList = (ListView) rootView.findViewById(R.id.lv_child_list);
            this.btAdd = (Button) rootView.findViewById(R.id.bt_add);
            this.title = (TextView) rootView.findViewById(R.id.tv_pop_title);
        }

    }

    @Override
    public void onPress(int primaryCode) {
    }

    @Override
    public void onRelease(int primaryCode) {
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeUp() {
    }

    @Override
    public void onFinishInput() {
        super.onFinishInput();

       /* String logString = getNowDate() + "," + getCurrentTimeMillis() + ",-,-,-,-,-,-,-,\n";
        try {
            OutputStream out = openFileOutput(LOG_FILE, MODE_APPEND);
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "SHIFT-JIS"));
            writer.append(logString);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        sb.setLength(0);
    }
    public void writeinphone(String text){

        final String PATH = Environment.getExternalStorageDirectory().toString() +File.separator +getApplicationContext().getPackageName();
        Log.d(getPackageName(), "Path:" + PATH);
        File f = new File(PATH);
        if (!f.exists() && !f.mkdir()) {
            return;
        }
        Log.d(getPackageName(), "Path:" + PATH);
        final String fileName = PATH + File.separator + "Record_public.txt";
        FileOutputStream osw = null;

        try {
            osw =new FileOutputStream(fileName,true);
            osw.write(text.getBytes("SHIFT_JIS"));



        } catch (FileNotFoundException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        } finally {
            try {
                if (osw != null)
                    osw.close();
            } catch (IOException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();


            }

            osw = null;
        }

    }
    public void Recoardsentiment(String text){

        final String PATH = Environment.getExternalStorageDirectory().toString() +File.separator +getApplicationContext().getPackageName();
        Log.d(getPackageName(), "Path:" + PATH);
        File f = new File(PATH);
        if (!f.exists() && !f.mkdir()) {
            return;
        }
        Log.d(getPackageName(), "Path:" + PATH);
        final String fileName = PATH + File.separator + "Record_private.txt";
        FileOutputStream osw = null;

        try {
            osw =new FileOutputStream(fileName,true);
            osw.write(text.getBytes("SHIFT_JIS"));



        } catch (FileNotFoundException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自動生成された catch ブロック
            e.printStackTrace();
        } finally {
            try {
                if (osw != null)
                    osw.close();
            } catch (IOException e) {
                // TODO 自動生成された catch ブロック
                e.printStackTrace();
            }

            osw = null;
        }

    }
    public String getdate(){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);// 0 - 11
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        String date = year + "/" + (month + 1) + "/" + day + "/" + hour + "/" + minute + "/" + second;
        return  date;
    }

    public static void removeDupElement(List list) {
        for(int i=0 ; i<list.size()- 1 ; i ++ ) {
            for(int j=list.size()-1;j> i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        System.out.println(list);
    }
    public String okurikana(String sb) {
        //System.out.println(sb);
        String newsb;
        if (sb.length() >= 2) {
            if (sb.charAt(sb.length() - 1) == 'あ') {
                newsb = sb.substring(0, sb.length() - 1) + "a";
            } else if (sb.charAt(sb.length() - 1) == 'い') {
                newsb = sb.substring(0, sb.length() - 1) + "i";
            } else if (sb.charAt(sb.length() - 1) == 'う') {
                newsb = sb.substring(0, sb.length() - 1) + "u";
            } else if (sb.charAt(sb.length() - 1) == 'え') {
                newsb = sb.substring(0, sb.length() - 1) + "e";
            } else if (sb.charAt(sb.length() - 1) == 'お') {
                newsb = sb.substring(0, sb.length() - 1) + "o";
            } else if (sb.charAt(sb.length() - 1) == 'か' || sb.charAt(sb.length() - 1) == 'き' || sb.charAt(sb.length() - 1) == 'く' || sb.charAt(sb.length() - 1) == 'け' || sb.charAt(sb.length() - 1) == 'こ') {
                newsb = sb.substring(0, sb.length() - 1) + "k";
            } else if (sb.charAt(sb.length() - 1) == 'さ' || sb.charAt(sb.length() - 1) == 'し' || sb.charAt(sb.length() - 1) == 'す' || sb.charAt(sb.length() - 1) == 'せ' || sb.charAt(sb.length() - 1) == 'そ') {
                newsb = sb.substring(0, sb.length() - 1) + "s";
            } else if (sb.charAt(sb.length() - 1) == 'た' || sb.charAt(sb.length() - 1) == 'ち' || sb.charAt(sb.length() - 1) == 'つ' || sb.charAt(sb.length() - 1) == 'て' || sb.charAt(sb.length() - 1) == 'と'|| sb.charAt(sb.length() - 1) == 'っ') {
                newsb = sb.substring(0, sb.length() - 1) + "t";
            } else if (sb.charAt(sb.length() - 1) == 'な' || sb.charAt(sb.length() - 1) == 'に' || sb.charAt(sb.length() - 1) == 'ぬ' || sb.charAt(sb.length() - 1) == 'ね' || sb.charAt(sb.length() - 1) == 'の') {
                newsb = sb.substring(0, sb.length() - 1) + "n";
            } else if (sb.charAt(sb.length() - 1) == 'は' || sb.charAt(sb.length() - 1) == 'ひ' || sb.charAt(sb.length() - 1) == 'ふ' || sb.charAt(sb.length() - 1) == 'へ' || sb.charAt(sb.length() - 1) == 'ほ') {
                newsb = sb.substring(0, sb.length() - 1) + "h";
            } else if (sb.charAt(sb.length() - 1) == 'ま' || sb.charAt(sb.length() - 1) == 'み' || sb.charAt(sb.length() - 1) == 'む' || sb.charAt(sb.length() - 1) == 'め' || sb.charAt(sb.length() - 1) == 'も') {
                newsb = sb.substring(0, sb.length() - 1) + "m";
            } else if (sb.charAt(sb.length() - 1) == 'が' || sb.charAt(sb.length() - 1) == 'ぎ' || sb.charAt(sb.length() - 1) == 'ぐ' || sb.charAt(sb.length() - 1) == 'げ' || sb.charAt(sb.length() - 1) == 'ご') {
                newsb = sb.substring(0, sb.length() - 1) + "g";
            } else if (sb.charAt(sb.length() - 1) == 'ざ' || sb.charAt(sb.length() - 1) == 'じ' || sb.charAt(sb.length() - 1) == 'ず' || sb.charAt(sb.length() - 1) == 'ぜ' || sb.charAt(sb.length() - 1) == 'ぞ') {
                newsb = sb.substring(0, sb.length() - 1) + "z";
            } else if (sb.charAt(sb.length() - 1) == 'だ' || sb.charAt(sb.length() - 1) == 'ぢ' || sb.charAt(sb.length() - 1) == 'づ' || sb.charAt(sb.length() - 1) == 'で' || sb.charAt(sb.length() - 1) == 'ど') {
                newsb = sb.substring(0, sb.length() - 1) + "d";
            } else if (sb.charAt(sb.length() - 1) == 'ば' || sb.charAt(sb.length() - 1) == 'び' || sb.charAt(sb.length() - 1) == 'ぶ' || sb.charAt(sb.length() - 1) == 'べ' || sb.charAt(sb.length() - 1) == 'ぼ') {
                newsb = sb.substring(0, sb.length() - 1) + "b";
            } else if (sb.charAt(sb.length() - 1) == 'ぱ' || sb.charAt(sb.length() - 1) == 'ぴ' || sb.charAt(sb.length() - 1) == 'ぴ' || sb.charAt(sb.length() - 1) == 'ぺ' || sb.charAt(sb.length() - 1) == 'ぽ') {
                newsb = sb.substring(0, sb.length() - 1) + "p";
            } else if (sb.charAt(sb.length() - 1) == 'ら' || sb.charAt(sb.length() - 1) == 'り' || sb.charAt(sb.length() - 1) == 'る' || sb.charAt(sb.length() - 1) == 'れ' || sb.charAt(sb.length() - 1) == 'ろ') {
                newsb = sb.substring(0, sb.length() - 1) + "r";
            } else if (sb.charAt(sb.length() - 1) == 'や' || sb.charAt(sb.length() - 1) == 'ゆ' || sb.charAt(sb.length() - 1) == 'よ') {
                newsb = sb.substring(0, sb.length() - 1) + "y";
            } else if (sb.charAt(sb.length() - 1) == 'わ' || sb.charAt(sb.length() - 1) == 'を') {
                newsb = sb.substring(0, sb.length() - 1) + "w";
            } else {
                newsb = null;
            }
        } else {
            newsb = null;
        }
        //System.out.println(newsb);
        return newsb;
    }




}